<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use \Common\Model\Goods;
use \Common\Model\GoodsDetail;
use \Common\Model\GoodsAttr;
use \Common\Model\TypeAttr;
use \Common\Model\GoodsList;
use \Common\Model\cate;
use \Common\Model\brand;

class ProductController extends Controller{
    
    public function index(){
    	//1.获取当前商品    	
		$gid = q('get.gid');
		$goodsData = Db::table('goods g')
					->join('goods_detail gd','g.gid','=','gd.goods_gid')
					->where('gid',$gid)
					->get();
					
		//将图片变成数组
	  	foreach($goodsData as $k=>$v){
	   		//把字符串变为数组，将来好循环
	   		$goodsData[$k]['picture'] = explode('|', $v['picture']);
	   	}	
		View::with('goodsData',$goodsData);
//p($goodsData);
		//2.获取当前商品属性
		$goodsAttr = Db::table('goods_attr ga')
						->join('type_attr ta','ga.type_attr_trid','=','ta.trid')
						->where("class=1 AND goods_gid={$gid}")
						->get();

		//2_1.相同属性归到同类
		$sameAttr = [];
		foreach($goodsAttr as $v){
			$sameAttr[$v['type_attr_trid']][] = $v;
		}

		//2_2.组合属性名字和值
		$sameType = [];
		$TypeAttrModel = new TypeAttr;			
		foreach($sameAttr as $k=>$v){
			$sameType[]= [
				'name'=>$TypeAttrModel->where('trid',$k)->pluck('trname'),
				'value'=>$v
			];
		}
		View::with('sameType',$sameType);

		//3.位置显示
		$cateModel = new cate;
		//顶级分类名称		
		$cid = q('get.cid');
		$cateName = $cateModel->where('cid',$cid)->pluck('cname');
		//三级分类名称
		$cidSon = $goodsData[0]['cate_cid'];
		$cateNameSon = $cateModel->where('cid',$cidSon)->pluck('cname');
		//品牌名称
		$brandModel = new brand;
		$bid = $goodsData[0]['brand_bid'];
		$brandName = $brandModel->where('bid',$bid)->pluck('bname');
		
		$nowname = [
			'0'  => $cateName,
			'1'  => $cateNameSon,
			'2'  => $brandName
		];
		View::with('nowname',$nowname);	
		
		//4.点击次数加1
		$goodsModel = new Goods;
		$goodsModel->where("gid={$gid}")->increment('times',1);
    	View::make();
    }
	
	//接收异步数据
	public function getAttr(){
		//接收前台发过来属性组合
		$combine = q('post.str');
		//去掉字符串最后一个字符 (,)
		$newCombine = substr($combine, 0,strlen($combine)-1);
		//查货品列表
		$goodslistModel = new GoodsList;
		$Data = $goodslistModel->where('combine',$newCombine)->find();	
		$goodslistData = $goodslistModel->where('combine',$newCombine)->find();	
		//获取带有附加价格的属性
		$goodsAttrModel = new GoodsAttr;
		//计算附加价格的总价格	
		$price = '';	
		foreach(explode(',', $newCombine) as $v){
			$price += $goodsAttrModel->where('gaid',$v)->pluck('add_price');
		}
		$goodsNum = q('post.gn');
		//增加附加价格字段
		$goodslistData['price'] = $price;		
		$goodslistData['goodsNum'] = $goodsNum;		
		//传递给js
		if($Data){
			echo json_encode($goodslistData);exit;
		}
	}
		
	//直接购买
	public function buy(){
		$gid = q('post.gid');
		$goodsModel = new  Goods;
		$goodsinfo = $goodsModel->where('gid',$gid)->find();
		//2.获取购买商品数量
		$goodsNum = q('post.gn');
    	//3.获取存入购物车商品信息
    	$combine = q('post.str');
		//去掉字符串最后一个字符 (,)
		$newCombine = substr($combine, 0,strlen($combine)-1);
		/*获取商品规格*/
		//连接商品属性和属性类型表
		$goodsAttr = Db::table('goods_attr ga')
							->join('type_attr ta','ga.type_attr_trid','=','ta.trid')
							->where("goods_gid={$gid} AND gaid IN({$newCombine})")
							->get();											
		//组合商品规格
		foreach($goodsAttr as $k=>$v){
			$options{$v['trname']} = $v['gvalue'];
		}
		//购物车添加 
        $data = [
            'id'        => $gid, 					//商品 ID
            'name'      => $goodsinfo['gname'],		//商品名称
            'num'       => $goodsNum, 				//商品数量
            'cid'		=> $goodsinfo['cate_cid'],	//商品cid
            'price'     => $goodsinfo['mallPrice'], //商品价格
            'picture'	=> $goodsinfo['pic'],		//商品图片
            'options'   => $options,				//商品规格
            'combine'   => $newCombine,				//属性组合id，用于订单列表
            'total'     => $goodsNum*$goodsinfo['mallPrice'], //总价
        ];		
		$_SESSION['shopbuy']['goods'] = $data;
		echo json_encode($data);exit;
	}
}
